package bidsanity



import grails.test.mixin.*
import org.junit.*

/**
 * See the API for {@link grails.test.mixin.services.ServiceUnitTestMixin} for usage instructions
 */
@TestFor(BidService)
@Mock([Customer, Listing, Bid, CustomerService, ListingService])
class BidServiceTests {
    def customerService = new CustomerService()
    def listingService = new ListingService()

    void testCreateBid() {
        Assert.assertNotNull(customerService)
        Assert.assertNotNull(listingService)
        def bidder = customerService.createCustomer(emailAddress: 'bidder@example.org', password: 'testing')
        def seller = customerService.createCustomer(emailAddress: 'seller@example.org', password: 'testing')
        Assert.assertNotNull(bidder.id)
        Assert.assertNotNull(seller.id)
        def listing = listingService.createListing(name: 'TestListing', endDate:  new Date() + 10, 
                                                   startingBidPrice: 1.0D, sellerId:  seller.id)
        Assert.assertNotNull(listing.id)
        
        def b = service.createBid(amount: 2.0D, bidderId: bidder.id, listingId: listing.id)
        Assert.assertNotNull(b)
        Assert.assertNotNull(b.id)
        Assert.assertFalse(b.hasErrors())
        Assert.assertFalse(listing.hasErrors())
    }
    
    void testCreateBidAfterExpired(){
        def bidder = customerService.createCustomer(emailAddress: 'bidder@example.org', password: 'testing')
        def seller = customerService.createCustomer(emailAddress: 'seller@example.org', password: 'testing')
        Assert.assertNotNull(bidder.id)
        Assert.assertNotNull(seller.id)
        def listing
        use( [groovy.time.TimeCategory] ){        
            listing = listingService.createListing(name: 'TestListing', endDate:  new Date() + 1.seconds,
                    startingBidPrice: 1.0D, sellerId:  seller.id)
            Assert.assertNotNull(listing.id)
        }
        
        Thread.sleep(2000)
        def b = service.createBid(amount: 2.0D, bidderId: bidder.id, listingId: listing.id)
        Assert.assertNull(b)

        
        
    }
    
}
